//
//  SYButtonMoreTVC.m
//  BSHOUYIFANG
//
//  Created by 曹雁彬 on 15/8/14.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYButtonMoreTVC.h"
#import "SYbuttonMoreTableViewCell.h"
#import "Header.h"
#import "MYRequestManger.h"
#import "SYbuttonMore.h"
#import "LORefresh.h"
#import "SYDetailViewController.h"
@interface SYButtonMoreTVC ()
@property(nonatomic,strong)NSMutableArray *dataSoure;
@property(nonatomic,assign)NSInteger temp1;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,assign)NSString *myID;
@end

@implementation SYButtonMoreTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationItemTitleColor];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    UIButton*leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30*0.6,30*0.6)];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem= leftItem;
    _index = 1;
    _dataSoure = [NSMutableArray array];
    _array = [NSMutableArray array];
    for (int i = 0; i<100; i++) {
        [_array addObject:@(i)];
    }
    [self postData];
    [self addLorefresh];
    [self.tableView registerClass:[SYbuttonMoreTableViewCell class] forCellReuseIdentifier:@"buttonMore"];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
}
//上拉加载
- (void)addLorefresh
{
    __weak SYButtonMoreTVC *product = self;
   // __weak NSMutableArray *arr = _dataSoure;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        _index+=1;
        [product postData];
    }];
    /*
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [arr removeAllObjects];
        _index = 1;
        [product postData];
    }];*/
}
- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:_titleStr];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = customLab;
}
- (void)searchprogram
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (NSString *)urlForButtonMoreWithTitle:(NSString *)title
{
   
    NSDictionary *dic = @{
                          @"圈儿推荐":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&mquan_id=556&F=android&V=1.4.0&key=1447071911477&channel=xiaomi&sign=bc5b32f901d6fbb15bd5419f&user_id=&page=1&module_id=9&data_from=more"
                          ,@"热门圈子":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&V=1.1.3&mquan_id=556&F=ios&key=1439558804768597881&user_id=&sign=617f3f70b8c4db8822e8ebf2&module_id=10&page="
                          ,@"新锐圈子":@"ttp://sdxapp.com/api/geta.html?act=GetIndexMoreData&mquan_id=556&F=android&V=1.4.0&key=1447072267288&channel=xiaomi&sign=85ad056027278db48ed660f2&user_id=&page=1&module_id=159&data_from=more"
                          ,@"美食资讯":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&mquan_id=556&F=android&V=1.4.0&key=1447072119477&channel=xiaomi&sign=4caaf1cadae1a0210c099579&user_id=&page=1&module_id=130&data_from=more"
                          ,@"编织热帖":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&mquan_id=556&F=android&V=1.4.0&key=1447072190560&channel=xiaomi&sign=4ca7b13f33fa2df9a2ead932&user_id=&page=1&module_id=12&data_from=more"
                          ,@"拈花惹草":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&V=1.1.3&mquan_id=556&F=ios&key=14396064664244927431&user_id=&sign=e74556f07862a95e4e287e7f&module_id=24&page="
                          ,@"粘土大作战":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&V=1.1.3&mquan_id=556&F=ios&key=14396064814076044021&user_id=&sign=1646af6dc23003b7bc5f6b69&module_id=26&page="
                          ,@"亲亲宝贝":@"http://sdxapp.com/api/geta.html?act=GetIndexMoreData&V=1.1.3&mquan_id=556&F=ios&key=14396064962416032973&user_id=&sign=c4af723c53f1ac4931c3eb0f&module_id=11&page="};
   
   NSString *url = [dic objectForKey:title];
   NSString *urlStr = [NSString stringWithFormat:@"page=%ld",_index];
    NSString *newUrl = [url stringByReplacingOccurrencesOfString:@"page=1" withString:urlStr];
    return newUrl;
}

- (void)postData
{
    [MYRequestManger manager:POST RequestWith:[self urlForButtonMoreWithTitle:_titleStr] parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *dic = dict[@"data"][@"data"];
        for (NSDictionary *dict1 in dic) {
            SYbuttonMore *model = [[SYbuttonMore alloc]init];
            [model setValuesForKeysWithDictionary:dict1];
            if ([model.img isEqualToString:@""]) {
                continue;
            }
            [self.dataSoure addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.defaultFooter endRefreshing];
        //[self.tableView.defaultHeader endRefreshing];
    } error:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.dataSoure.count/2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT/5-5;
}
- (void)goToWebView
{
    SYDetailViewController *detailVC = [[SYDetailViewController alloc]init];
    // NSLog(@"%@",_myID);
    detailVC.myID = _myID;
    detailVC.myBool = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
- (void)goToSecond
{
    self.tabBarController.selectedIndex = 1 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYbuttonMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buttonMore" forIndexPath:indexPath];
   
    if (indexPath.row*2+1 <=self.dataSoure.count -1)
    {
        SYbuttonMore *model1 = self.dataSoure[indexPath.row+[self.array[indexPath.row] intValue]];
        SYbuttonMore *model2 = self.dataSoure[indexPath.row+[self.array[indexPath.row] intValue]+1];
        cell.homeWebBlock = ^(NSString *myID){
            _myID = myID;
            NSString *str =[_titleStr substringWithRange:NSMakeRange(2, 1)];
            
            if (![str isEqualToString:@"圈"]) {
            [self goToWebView];
            }else
            {
                [self goToSecond];
            }
           
           
        };
        cell.model1 = model1;
        cell.model2 = model2;
    }
    
    return cell;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
